package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.RunfastDeliverCost;
import com.runfast.paotui.dao.model.RunfastLoginRecord;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastDeliverCostService;
import com.runfast.paotui.service.RunfastLoginRecordService;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.waimai.util.AESUtil;
import com.runfast.waimai.util.ComUtil;
import com.runfast.waimai.util.Constant;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shopper/*")
public class ShopperTokenLoginController  {
    @Resource
    RunfastShopperService shopperService;
    @Resource
    RunfastLoginRecordService runfastLoginRecordService;
    @Resource
    private RunfastDeliverCostService deliverCostService;


    @PostMapping("token")
    public @ResponseBody ModelMap token(String number , String password, HttpSession session,
                                     @RequestParam(value="userName",required=false) String userName,
                                     @RequestParam(value="vercode",required=false) Integer vercode,
                                        @RequestParam(value="bdchannelId",required=false) String bdchannelId,
                                        @RequestParam(value="bduserId",required=false) String bduserId,
                                        @RequestParam(value="pushType",required=false) Integer pushType,
                                        @RequestParam(value="otherId",required=false) String otherId,
                                        @RequestParam(value="bptype",required=false,defaultValue="0") Integer bptype,
                                        @RequestParam(value="alias",required=false) String alias ) {
        String a = "abcdefghijklmn";
        ModelMap model = new ModelMap();

        RunfastShopper shopper = shopperService.findByNumeber(number);
        if (shopper != null) {
            if (DigestUtils.md5Hex(password).equals(shopper.getPassword())) {
                model.put("msg", "密码相同");
                String token1 = AESUtil.encrypt(String.valueOf(shopper.getId()), a);//骑手id和密码生成token1
                shopper.setToken(token1);//设置token1
                shopperService.addtoken(shopper);//更新到数据库
                session.setAttribute("token", token1);//保存到session
                RunfastLoginRecord shop=new RunfastLoginRecord();
                shop.setAccountid(shopper.getId()); //设置骑手ID
                shop.setLogintime(new Date());//设置时间
                shop.setAccounttype(2);//设置骑手类型
                shop.setAlias(alias);//设置识别码
                runfastLoginRecordService.insertSelective(shop); //把数据添加进去
                    model.put("token", token1);//返回前端
                    model.put("sucess", "登陸成功");

                }else { model.put("msg","登陸不成功");}


            }
            return model;

        }


    @PostMapping("tokenlogin")
    public @ResponseBody ModelMap tokenlogin(String token,HttpServletRequest request,
                                             HttpServletResponse response){

        String a="abcdefghijklmn";
        ModelMap model=new ModelMap();
    //   String token1= request.getParameter(token2);//从前端上获取token,试试

       String shopperid= AESUtil.decrypt(token,a); //解码

       if(shopperid==null){
           model.put("msg","token已失效");

       }else {
          RunfastShopper shopper = shopperService.get(Integer.parseInt(shopperid));
          Integer id=Integer.parseInt(AESUtil.decrypt(token,a)); //注意不能直接比较，不然很容易出不来结果 最好是像这样转换一下

          if(id==shopper.getId()){


    //          runfastLoginRecordService.getshopperLoginmsg(shop.getAccountid());

              model.put("sucess","成功着陆");

              model.put("前端传过来的token",token);

        //      model.put("shopperid",AESUtil.decrypt(token,a));


          }
       }
       return model;
    }
}
